# Python setattr()、getattr()、hasattr()函数用法详解
## hasattr()函数
'''
hasattr(obj, name)
'''
class CLanguage:
    def __init__(self):
        self.name = "C语言中文网"
        self.add = "http://c.biancheng.net"
    
    def say(self):
        print("我正在学习Python")

clangs = CLanguage()
print(hasattr(clangs, "name"))
print(hasattr(clangs, "add"))
print(hasattr(clangs, "say"))
print()

## getattr()函数
'''
getattr(obj, name[, default])
'''
class CLanguage:
    def __init__(self):
        self.name = "C语言中文网"
        self.add = "http://c.biancheng.net"
    
    def say(self):
        print("我正在学Python")

clangs = CLanguage()
print(getattr(clangs, "name"))
print(getattr(clangs, "add"))
print(getattr(clangs, "say"))
print(getattr(clangs, "display", "display"))
print()

## setattr()函数
'''
setattr(obj, name, value)
'''
class CLanguage:
    def __init__(self):
        self.name = "C语言中文网"
        self.add = "http://c.biancheng.net"
    
    def say(self):
        print("我正在学Python")

clangs = CLanguage()
print(clangs.name)
print(clangs.add)
setattr(clangs, "name", "Python教程")
setattr(clangs, "add", "http://c.biancheng.net/python/")
print(clangs.name)
print(clangs.add)
print()

def say():
    print("我正在学Python")

class CLanguage:
    def __init__(self):
        self.name = "C语言中文网"
        self.add = "http://c.biancheng.net"

clangs = CLanguage()
print(clangs.name)
print(clangs.add)
setattr(clangs, "name", say)
clangs.name()
print()

def say(self):
    print("我正在学Python")

class CLanguage:
    pass

clangs = CLanguage()
setattr(clangs, "name", "C语言中文网")
setattr(clangs, "say", say)
print(clangs.name)
clangs.say(clangs)
